package Questions;

/**
 * @version 3 18 February 2013
 * @author Myhaylo Kotsyuruba
 */

public class BandsQuestion extends Question {
    private int[] answers;

    public BandsQuestion(String text, int score, int[] answers) {
        super(text, score);
        this.answers = answers;
    }

    @Override
    public Question clone() {
        return new BandsQuestion(getText(), getScore(), answers);
    }

    @Override
    public String toString() {
        return String.format("%s%nPlease, type Your answer (number).",
                getText());
    }
    
    public static Question createQuestion(String data) {
        String[] fields = data.split(FIELDS_SEPARATOR);
        int[] answers = new int[fields.length - 2];
        for (int index = 0; index < answers.length; index++) {
            answers[index] = Integer.parseInt(fields[index + 2]);
        }
        return new BandsQuestion(fields[0], Integer.parseInt(fields[1]),
                                 answers);
    }

    @Override
    public int rateUserAnswer(String userAnswer) {
        int userNumber = Integer.parseInt(userAnswer);
        if (userNumber == answers[0]) {
            return getScore();
        }
        int diapason = 0;
        for (int index = 1; index < answers.length; index++) {
            if (Math.abs(answers[0] - userNumber) <= answers[index]) {
                diapason++;
            }
        }
        return getScore() * diapason / answers.length;
    }
}
